#include <include/types.h>
#include <include/task.h>
#include <include/limits.h>
#include <include/string.h>
#include <include/system.h>
#include <include/stdarg.h>
#include <include/io.h>
#include <dev/console.h>
#include <mm/memory.h>



task_struct  task_list[MAX_TASK];        /* 任务队列 */
timer_struct timer_list[MAX_TIMER];      /* 定时器队列 */
u32  current;                            /* 当前任务 */
u32  jiffies;                            /* 开机后的滴答数 */

char printbuff[1024];

static int printf(const char *fmt, ...)
{
    va_list args;
    int i,j;
    char c[2]={0,0};

    va_start(args, fmt);
    i = vsprintf(printbuff, fmt, args);
    va_end(args);

    for (j = 0; j < i; ++j)
    {
        c[0] = printbuff[j];
        c[1] = 7;
        write_con(c, 1);
    }
    return i;
}

void timer_handler(int cpl)
{
    ++jiffies;
    cmos_time time;
    cmos_date date;                          

    goto_xy(0,24);
    printf("Clock tick count %ld", jiffies);
    /* DEBUG */
    if (jiffies%100 == 0)
    {
        time.sec =  readcmos(0);
        time.min =  readcmos(2);
        time.hour=  readcmos(4);
        BCD_TO_BIN(time.sec);
        BCD_TO_BIN(time.min);
        BCD_TO_BIN(time.hour);

        date.dayofweek =readcmos(6);
        date.day =readcmos(7);
        date.month =readcmos(8);
        date.year = readcmos(9);
        BCD_TO_BIN(date.day);
        BCD_TO_BIN(date.month);
        BCD_TO_BIN(date.year);
        date.year+=2000;
        goto_xy(0,25);
        printf("%04d-%02d-%02d %d %02d:%02d:%02d ", 
            date.year,
            date.month,
            date.day,
            date.dayofweek,
            time.hour,
            time.min,
            time.sec
            );
    }
    
    if (3 == cpl) {                         //对进程进行计时
        task_list[current].user_time++;     //用户态的执行时间
    } else {
        task_list[current].system_time++;   //内核态的执行时间
    }    

    //spring_timer();                         //触发定时器
    
    outb_p(0x20, 0x20);

    if ((--task_list[current].counter) <= 0) 
    {                                       //如果时间片被用完则需要进行调度
        task_list[current].counter = 0; 
        //need_resched = 1;   
    } else {                                //如果时间片没有用完则继续执行
        return;
    }
    
    if (cpl == 3) {                         //如果进程在用户态下被中断
        //scheduler();                        //进行调度
    }
}
/* 初始化任务队列，定时器队列，系统滴答数 */
extern void init_task()
{
    int i;
    for (i = 0; i < MAX_TIMER; i++)
    {
        timer_list[i].state = TIMER_NULL;       /* 将所有定时器置为空 */
    }
    for (i = 0; i < MAX_TASK; i++)
    {
        task_list[i].state = TASK_NULL;         /* 将任务队列中所有项置为空 */
    }

    current = 0;                                /* 捏造一个0号任务 */
    task_list[0].state = TASK_RUNNING;            
    task_list[0].counter = 1;                   /* 其实不需要时间片，因为没有出现第二个任务 */
    task_list[0].user_time = 0;
    task_list[0].system_time = 0;
    task_list[0].start_time = 0;
    task_list[0].pgd = PAGE_DIR;
    strcpy(task_list[0].path, "/boot/oskernel");

    jiffies = 0;                                /* 滴答声初始化 */


    //set_system_gate(0x80,&system_call);
}
